package com.jchen.system.controller;

import com.jchen.common.constant.QueueConstant;
import com.jchen.common.util.R;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description
 * @Author jchen
 * @Date 2023/12/13 10:28
 */

@RestController
@RequestMapping("/mq")
@Api(value = "mq消息队列",tags = "mq消息队列")
public class SendMessageController {

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public R sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "这是一条测试消息, 你好啊骚年！";
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", new Date());
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(QueueConstant.JCHEN_DIRECT_EXCHANGE, QueueConstant.JCHEN_ROUTING, map);
        return R.ok();
    }

    @GetMapping("/sendSysDirectMessage")
    public R sendSysDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "这是一条系统消息, hello word！";
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", new Date());
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(QueueConstant.SYS_DIRECT_EXCHANGE, QueueConstant.SYS_ROUTING, map);
        return R.ok();
    }
}
